/*
 * Copyright (c) 2007 jNetX.
 * http://www.jnetx.com
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * jNetX. You shall not disclose such Confidential Information and
 * shall use it only in accordance with the terms of the license
 * agreement you entered into with jNetX.
 *
 * $Id$
 */
package com.jnetx.javax.sip.message;

import javax.sip.message.Response;
import java.text.ParseException;

/**
 * @author <a href="mailto:dparhonin@jnetx.ru">Dmitry Parhonin</a>
 * @version $Revision$
 */
public class SIPResponse extends SIPMessage implements Response {
    private int statusCode;
    private String reasonPhrase;

    public SIPResponse() {
    }

    protected SIPResponse(int numberOfHeaderNames) {
        super(numberOfHeaderNames);
    }

    /**
     * Creates and returns a deep copy of the Message. This methods must ensure a
     * deep copy of the message, so that it can be modified without effecting
     * the original message. This provides useful functionality for proxying
     * Requests and Responses, for example:
     * <ul>
     * <li>Recieve a message.
     * <li>Create a deep clone of the message.
     * <li>Modify necessary headers.
     * <li>Proxy the message using the send methods on the SipProvider.
     * </ul>
     * The message contents are cloned as follows:
     * <ul>
     * <li>If the content is of type byte[] a new byte[] array is allocated and the
     * original contents are copied over to the cloned Message.
     * <li>If the content is of type String then a new String equal to the old
     * String is allocated and assigned to the cloned Message.
     * <li>If the content is of type Object and it has a public clone method
     * then it is invoked and the resultant Object is used in the new cloned
     * Message.
     * </ul>
     *
     * @return a deep copy of Message
     */
    public Object clone() {
        final SIPResponse request = new SIPResponse(getNumberOfHeaderNames());
        request.cloneInternals(this);
        request.cloneHeaders(this);
        request.cloneContent(this);
        request.statusCode = this.statusCode;
        request.reasonPhrase = this.reasonPhrase;
        return request;
    }

    protected byte[] buildMessageData() {
        final StringBuilder sb = new StringBuilder();
        sb.append(getSIPVersion()).append(' ').append(Integer.toString(statusCode)).append(' ').append(reasonPhrase).append('\r').append('\n');
        buildMessageHeaders(sb);
        sb.append('\r').append('\n');
        buildMessageContent(sb);
        return sb.toString().getBytes();
    }

    /**
     * Sets the status-code of Response. The status-code is a 3-digit integer
     * result code that indicates the outcome of an attempt to understand and
     * satisfy a request.  The Status-Code is intended for use by automata.
     *
     * @param statusCode the new integer value of the status code.
     * @throws java.text.ParseException which signals that an error has been reached
     *                                  unexpectedly while parsing the statusCode value.
     */
    public void setStatusCode(int statusCode) throws ParseException {
        this.statusCode = statusCode;
    }

    /**
     * Gets the integer value of the status code of Response, which identifies
     * the outcome of the request to which this response is related.
     *
     * @return the integer status-code of this Response message.
     */
    public int getStatusCode() {
        return statusCode;
    }

    /**
     * Sets reason phrase of Response. The reason-phrase is intended to give a
     * short textual description of the status-code. The reason-phrase is
     * intended for the human user. A client is not required to examine or
     * display the reason-phrase. While RFC3261 suggests specific wording for
     * the reason phrase, implementations MAY choose other text.
     *
     * @param reasonPhrase the new string value of the reason phrase.
     * @throws java.text.ParseException which signals that an error has been reached
     *                                  unexpectedly while parsing the reasonPhrase value.
     */
    public void setReasonPhrase(String reasonPhrase) throws ParseException {
        this.reasonPhrase = reasonPhrase;
    }

    /**
     * Gets the reason phrase of this Response message.
     *
     * @return the string value of the reason phrase of this Response message.
     */
    public String getReasonPhrase() {
        return reasonPhrase;
    }

    /**
     * This method parses the {@link #messageData}. It is obliged to reset {@link #parsed} indicator.
     */
    protected void parse() {
        //todo Implement parsing
        parsed = true;
    }
}
/*
 * $Log$
 */